﻿/*
 * Copyright 2012 Rene Prost, Registrite ja Infosüsteemide Keskus 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
using System;
using System.Web;
using NUnit.Framework;
using System.IO;
 
using Xtee.Core.Adapter;

using Xtee.Core.Client;
using Xtee.Core.Mime;

namespace Xtee.Core.Test.Adapter
{
   

   
    [TestFixture]
    public class XteeHttpHandlerTest : TestBase
    {
        [Test]
        public void HandlerShouldReturnTechFault()
        {
            XteeServiceHttpHandler handler = new XteeServiceHttpHandler();
            const string xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'/>";

            HttpContext context = GetTestContext(xml, "text/xml");
            handler.ProcessRequest(context);
            context.Response.Flush();

            ContentType ct = new ContentType(context.Response.ContentType);
            Assert.AreEqual("text/xml", ct.MediaType);

            var outputText = output.ToString();
            Assert.IsTrue(outputText.Contains("<SOAP-ENV:Body><SOAP-ENV:Fault><faultcode>"));
        }
       
        [Test]
        public void HandlerShouldReturnListMethods()
        {
          
            HttpContext context = GetTestContext(File.ReadAllText(@"../../ResourceData/listMethods.xml"), "text/xml");
            new XteeServiceHttpHandler().ProcessRequest(context); 
            context.Response.Flush();

            var ct  = new ContentType(context.Response.ContentType);
            Assert.AreEqual("text/xml",ct.MediaType);
            Console.WriteLine(output);
            Assert.IsTrue(output.ToString().Contains("listMethodsResponse"));
            Assert.IsTrue(output.ToString().Contains("<id:subsystemCode>Calc</id:subsystemCode>"));
            Assert.IsTrue(output.ToString().Contains("<id:serviceCode>Add</id:serviceCode>"));
            
        }
        //[Ignore]
        //[Test]
        //public void HandlerShouldBeAbleToListMethods_Per_Producer()
        //{
        //    HttpContext context = GetTestContext(File.ReadAllText(@"../../ResourceData/listMethods.xml"), "text/xml", "producer=kis");
        //    new XteeServiceHttpHandler().ProcessRequest(context);
        //    context.Response.Flush(); 
        //    Console.WriteLine(output);
        //     var outv =
        //    output.ToString();
        //     Assert.IsTrue(outv.Contains("listMethodsResponse"));
        //     Assert.IsTrue(outv.Contains("kis."));
        //    Assert.IsFalse(output.ToString().Contains("producerX."));
        //}
        //[Ignore]
        //[Test]
        //public void HandlerShouldReturnListMethods_By_AlternateProducer()
        //{
        //    var handler = new XteeServiceHttpHandler();
        //    //TODO:
        //    HttpContext context = GetTestContext(
        //        File.ReadAllText(@"../../ResourceData/listMethods.xml"), "text/xml", "producer=kis_arendus_test");
        //    handler.ProcessRequest(context);

        //    context.Response.Flush();


        //    ContentType ct = new ContentType(context.Response.ContentType);
        //    Assert.AreEqual("text/xml", ct.MediaType);
        //    Console.WriteLine(output);
        //    Assert.IsTrue(output.ToString().Contains("listMethodsResponse"));
        //    Assert.IsTrue(output.ToString().Contains("kis_arendus_test."));

        //}

        [Test]
        public void HandlerShouldPassToLocalProcessing()
        {
            XteeServiceHttpHandler handler = new XteeServiceHttpHandler();
            const string xml = @"<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xrd='http://x-road.eu/xsd/xroad.xsd' xmlns:id='http://x-road.eu/xsd/identifiers' xmlns:tns='http://calculator.x-road.eu'>
<SOAP-ENV:Header>
<xrd:client id:objectType='SUBSYSTEM'>
 <id:xRoadInstance>ee-dev</id:xRoadInstance>
 <id:memberClass>GOV</id:memberClass>
 <id:memberCode>70000310</id:memberCode>
<id:subsystemCode>generic-consumer</id:subsystemCode>
 </xrd:client>
 <xrd:service id:objectType='SERVICE'>
 <id:xRoadInstance>EE</id:xRoadInstance>
 <id:memberClass>GOV</id:memberClass>
 <id:memberCode>700030</id:memberCode>
<id:subsystemCode>Calc</id:subsystemCode>
 <id:serviceCode>Add</id:serviceCode>
<id:serviceVersion>v1</id:serviceVersion>
 </xrd:service>
 <xrd:id>411d6755661409fed365ad8135f8210be07613da</xrd:id>
<xrd:userId>EE39010210022</xrd:userId>
 <xrd:protocolVersion>4.0</xrd:protocolVersion>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<tns:Add>
<request >
<VarX>1</VarX>
<VarY>1</VarY>
</request>
</tns:Add>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
";

            HttpContext context = GetTestContext(xml,"text/xml");
            handler.ProcessRequest(context);
            context.Response.Flush();

            ContentType ct = new ContentType(context.Response.ContentType);
            Assert.AreEqual("text/xml", ct.MediaType);
            Console.WriteLine(output);
            Assert.IsTrue(output.ToString().Contains("<Result><Value>2</Value></Result></tns:AddResponse>"));

        }
        
    }
}
